package com.github.binarywang.demo.wx.mp.controller;

import com.github.binarywang.demo.wx.mp.beans.HaoZhu;
import com.github.binarywang.demo.wx.mp.dto.Result;
import com.github.binarywang.demo.wx.mp.service.Haozhuservice;
import com.github.binarywang.demo.wx.mp.service.SmsService;
import com.github.binarywang.demo.wx.mp.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Api(tags = "商户注册和登录")
@Controller
public class LoginController {
    @Autowired
    Haozhuservice hs;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    RedisUtils redisUtils=new RedisUtils();
    @ResponseBody
    @PostMapping("/register")
    public Result<HaoZhu> register(@RequestBody HaoZhu hz) {
       return hs.registerHZ(hz);
    }

    @ResponseBody
    @PostMapping("/login")
    public Result<HaoZhu> login(@RequestBody HaoZhu haoZhu) {
        return hs.loginHZ(haoZhu);
    }


    @Resource
    SmsService smsService;

    @ResponseBody
    @PostMapping("/sendSms")
    @ApiOperation(value="手机验证码",notes="phone：输入手机号码")
    public Result companyApproveList(@RequestParam(value = "phone") String phone){
        if (confPhone(phone)){//验证手机号码是否正确
            //这里生成六位随机数
            Integer random=(int)((Math.random()*9+1)*100000);
            String code=random.toString();

            if(smsService.sendVerificationCode(phone,code)){
                //发送后存到redis中，等待校验
                redisTemplate.opsForValue().set(phone, code, 300, TimeUnit.SECONDS);//将验证码存入缓存,5分钟后失效
                return Result.success("验证码："+code);
            }

            return null;
        }else {
            return Result.error("请填写正确的手机号");
        }
    }

    //验证手机号码是否正确
    public static boolean confPhone(String phone){
        if (phone.length() != 11)
        {
            return false;
        }else{
            /*
             * 移动号段正则表达式
             */
            String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
            /*
             * 联通号段正则表达式
             */
            String pat2  = "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
            /*
             * 电信号段正则表达式
             */
            String pat3  = "^((133)|(153)|(177)|(18[0,1,9])|(149))\\d{8}$";
            /*
             * 虚拟运营商正则表达式
             */

            String pat4 = "^((170))\\d{8}|(1718)|(1719)\\d{7}$";

            Pattern pattern1 = Pattern.compile(pat1);
            Matcher match1 = pattern1.matcher(phone);
            boolean isMatch1 = match1.matches();
            if(isMatch1){
                return true;
            }
            Pattern pattern2 = Pattern.compile(pat2);
            Matcher match2 = pattern2.matcher(phone);
            boolean isMatch2 = match2.matches();
            if(isMatch2){
                return true;
            }
            Pattern pattern3 = Pattern.compile(pat3);
            Matcher match3 = pattern3.matcher(phone);
            boolean isMatch3 = match3.matches();
            if(isMatch3){
                return true;
            }
            Pattern pattern4 = Pattern.compile(pat4);
            Matcher match4 = pattern4.matcher(phone);
            boolean isMatch4 = match4.matches();
            if(isMatch4){
                return true;
            }
            return false;
        }
    }
}

